package com.xiaoyao.myframeapp.MvpTest.model;


import com.xiaoyao.myframeapp.FrameApplication;
import com.xiaoyao.myframeapp.MvpTest.bean.Test;
import com.xiaoyao.myframeapp.core.BaseEntity;
import com.xiaoyao.myframeapp.core.subscriber.BaseObserver;
import com.xiaoyao.myframeapp.mvp.base.BaseActivity;
import com.xiaoyao.myframeapp.mvp.base.BaseModel;
import com.xiaoyao.myframeapp.utils.L;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author xiaoyao
 * @mail xiao80206040@qq.com
 * @explain
 * @Created by Administrator on 2017/2/27 0027.
 */
public class TestModel extends BaseModel {


    public void login(
            BaseActivity activity,
            String userId,
            String password,
            final InfoHint infoHint) {

        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");

        httpService.getVideoUrl(
//                userId,
//                password
        )
                // 在IO线程进行网络请求
                .subscribeOn(Schedulers.io())
                // 回到主线程 处理请求结果
                .observeOn(AndroidSchedulers.mainThread())

//                .compose(activity.<BaseEntity<Test>>bindUntilEvent(ActivityEvent.DESTROY))//解决
                //TODO bindToLifecycle的自动取消订阅示例，因为是在onStart的时候调用，所以在onStop的时候自动取消订阅
                //设置 被观察者
                .compose(activity.<BaseEntity<Test>>bindToLifecycle())//解决
//                .compose(getIView().<BaseEntity<Test>>setThread())//销毁Activity销毁 返回报错  空指针
//                .compose(RxScheduler1s.<BaseEntity<Test>>compose())//完成返回Activity 空指针
                //设置 观察者
                .subscribe(new BaseObserver<Test>(FrameApplication.getmContext()) {

                               @Override
                               protected void onHandleSuccess(Test test) {
                                   L.e("TestPresenter", "onHandleSuccess");
                                   infoHint.success(test);
                                   L.e("TestPresenter", "onHandleSuccess");
                               }

                               @Override
                               protected void onHandleError(String msg) {
                                   super.onHandleError(msg);
                                   infoHint.fail(msg);
                                   L.e("TestPresenter" + "onHandleError");
                               }
                           }
                );
        Observable<BaseEntity<Test>> observable = httpService.getVideoUrl();

        observable.subscribeOn(Schedulers.io())               // 在IO线程进行网络请求
                .observeOn(AndroidSchedulers.mainThread())  // 回到主线程 处理请求结果
                .subscribe(new Observer<BaseEntity<Test>>() {

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseEntity<Test> testBaseEntity) {

                    }


                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


    //通过接口产生信息回调
    public interface InfoHint {
        void success(Test isData);

        void fail(String str);

    }
}
